package cn.tedu.rms.user.controller;

import cn.tedu.rms.base.response.JsonResult;
import cn.tedu.rms.user.pojo.dto.UserLoginParam;
import cn.tedu.rms.user.pojo.dto.UserQuery;
import cn.tedu.rms.user.pojo.dto.UserSaveParam;
import cn.tedu.rms.user.pojo.vo.UserVO;
import cn.tedu.rms.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "用户管理模块")
@RequestMapping("v1/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation("顾客注册")
    @PostMapping("singLn")
    public JsonResult singLn(UserSaveParam userSaveParam){
        log.debug("用户注册:参数={}",userSaveParam);
        userService.singLn(userSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("用户登录:参数={}",userLoginParam);
        //调用业务层的login方法
        //Ctrl去接口  Ctrl+Alt去接口实现类  Ctrl+Shift去方法返回值类
        UserVO userVO = userService.login(userLoginParam);
        //将业务层返回的userVO对象封装到JsonResult中返回给前端
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "查询员工")
    @GetMapping("/select")
    public JsonResult select(UserQuery userQuery){
        log.debug("查询员工:参数={}",userQuery);
        List<UserVO> list=userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "保存 （新增/修改）员工")
    @PostMapping("/save")
    public JsonResult save(UserSaveParam userSaveParam){
        log.debug("保存员工:参数={}",userSaveParam);
        userService.save(userSaveParam);
        return JsonResult.ok();
    }
    @ApiOperation(value = "重置密码")
    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("重置密码:参数={}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改用户状态")
    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult updateStatus(
            @PathVariable Long userId,@PathVariable String status){
        log.debug("修改用户状态:参数={},{}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @ApiOperation(value = "删除用户")
    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户:参数={}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    // 登录通过手机号获取验证码
    @ApiOperation(value = "登录通过手机号获取验证码")
    @PostMapping("/getVerificationCode/{phone}")
    public JsonResult getVerificationCode(@PathVariable String phone){
        log.debug("登录通过手机号获取验证码:参数={}",phone);
        UserVO userVO = userService.getVerificationCode(phone);
        return JsonResult.ok(userVO);
    }

}
